/*
Created By VietRise on 11/6/2014
*/

#include "Bomb.h"
#include "Scenes\ScreenGameplay.h"

CCAction* Bomb::_appear = NULL;
CCAction* Bomb::_disappear = NULL;
CCAction* Bomb::_effectWasHit = NULL;

Bomb::~Bomb()
{
	CC_SAFE_RELEASE(_appear);
	CC_SAFE_RELEASE(_disappear);
}

Bomb::Bomb(ScreenGameplay* game)
{
	_game = game;
	_canHit = false;
	_atHole = -1;
	_timeAppear = 0;
	_timeDelay = 0;
	_countTimeDelay = 0;
	this->initWithSpriteFrameName("obj_bom_05.png");
	this->setVisible(false);
}

Bomb* Bomb::create(ScreenGameplay* game)
{
	Bomb* sprite = new Bomb(game);
	if (sprite)
	{
		sprite->autorelease();
		return sprite;
	}

	CC_SAFE_DELETE(sprite);
	return NULL;
}

void Bomb::createAction()
{
	// Action Appear
	CCAnimation* animation;
	animation = CCAnimation::create();
	CCSpriteFrame* frame;
	for (int i = 1; i <= 5; i++)
	{
		char szName[100] = { 0 };
		sprintf(szName, "obj_bom_0%i.png", i);
		frame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(szName);
		animation->addSpriteFrame(frame);
	}

	animation->setDelayPerUnit(TIME_ANIMATION_BOMB / 5);
	animation->setRestoreOriginalFrame(true);
	_appear = CCAnimate::create(animation);
	_appear->retain();

	// Action Disappear
	_disappear = CCDelayTime::create(TIME_DISAPPEAR_BOMB);
	_disappear->retain();

	// Action EffectWasHit
	CCFadeOut* fadeOut = CCFadeOut::create(0.3f);
	CCDelayTime* delay = CCDelayTime::create(0.1f);
	CCFadeIn* fadeIn = CCFadeIn::create(0.3f);
	CCFiniteTimeAction* sequence = CCSequence::create(fadeOut, delay, fadeIn, delay, NULL);
	_effectWasHit = CCRepeatForever::create((CCActionInterval*)sequence);
	_effectWasHit->retain();
}

void Bomb::appear(int atHole)
{
	this->setVisible(true);
	_canHit = true;
	_atHole = atHole;

	this->setRotation(0);
	this->setOpacity(255);
	this->setScale(1);

	this->stopAllActions();
	CCFiniteTimeAction* sequence = CCSequence::create(
		(CCAnimate*)Bomb::_appear->copy()->autorelease(),
		CCDelayTime::create(_timeAppear - TIME_ANIMATION_BOMB),
		CCCallFuncN::create(this, callfuncN_selector(Bomb::animationAppearDone)),
		NULL);

	this->runAction(sequence);
}

void Bomb::effectWasHit()
{
	_canHit = false;	
	this->stopAllActions();
	this->setDisplayFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("obj_bom_05.png"));
	CCArray* children = this->getChildren();
	CCSprite* sprite;	
	for (int i = 0; i < children->count(); i++)
	{
		sprite = (CCSprite*)children->objectAtIndex(i);
		sprite->setVisible(true);
		if (sprite->getTag() == kSpriteObjRay)
		{
			sprite->setOpacity(100 + rand() % 155);
			sprite->runAction((CCRepeatForever*)Bomb::_effectWasHit->copy()->autorelease());
		}
	}	

	CCFiniteTimeAction* sequence = CCSequence::create(
		CCDelayTime::create(TIME_EFFECT_BOMB),
		CCCallFuncN::create(this, callfuncN_selector(Bomb::bombExplose)),
		NULL
		);
	this->runAction(sequence);
}

void Bomb::animationAppearDone(CCNode* pSender)
{
	_canHit = false;

	this->setDisplayFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("obj_bom_06.png"));
	CCFiniteTimeAction* sequence = CCSequence::create(
		(CCDelayTime*)_disappear->copy()->autorelease(),
		CCCallFuncN::create(this, callfuncN_selector(Bomb::bombHide)),
		NULL);
	this->runAction(sequence);
}

void Bomb::bombHide(CCNode* pSender)
{
	pSender->setVisible(false);	
	this->setDisplayFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("obj_bom_05.png"));
	_game->setHoleBlank(_atHole);
	_atHole = -1;
}

void Bomb::bombExplose(CCNode* pSender)
{
	pSender->setVisible(false);
	this->setDisplayFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("obj_bom_05.png"));
	CCArray* children = this->getChildren();
	CCSprite* sprite;
	for (int i = 0; i < children->count(); i++)
	{
		sprite = (CCSprite*)children->objectAtIndex(i);
		sprite->setVisible(false);
		if (sprite->getTag() == kSpriteObjRay)
		{
			sprite->stopAllActions();
		}
	}

	// GameOver
	_game->loseGame();
}

float Bomb::randomFloat(float a, float b)
{
	float random = CCRANDOM_0_1();
	float diff = b - a;
	float r = random * diff;
	return a + r;
}

void Bomb::initTimeDelay(float timeDelay1, float timeDelay2)
{
	_timeDelay = randomFloat(timeDelay1, timeDelay2);
}

void Bomb::initTimeAppear(float timeAppear1, float timeAppear2)
{
	_timeAppear = randomFloat(timeAppear1, timeAppear2);
}

